package com.x.mygulimall5.authserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.x.mygulimall5.authserver.mapper.MemberMapper;
import com.x.mygulimall5.authserver.service.MemberService;
import com.x.mygulimall5.common.entity.member.MemberEntity;
import com.x.mygulimall5.common.exception.MyException;
import com.x.mygulimall5.common.utils.JwtUtils;
import com.x.mygulimall5.common.vo.member.MemberLoginVO;
import com.x.mygulimall5.common.vo.member.MemberRegisterVO;

public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {
    @Override
    public String login(MemberLoginVO memberLoginVO) {
        //校验：输入参数是否合法
        String mobile = memberLoginVO.getMobile(); //手机号
        String password = memberLoginVO.getPassword(); //密码
        if(StringUtils.isEmpty(mobile) ||StringUtils.isEmpty(password)){
            throw new MyException("输入参数为空",20005);
        }
        //校验手机号是否存在
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        MemberEntity member = baseMapper.selectOne(queryWrapper);
        if(member == null){
            throw new MyException("手机号不正确",20006);
        }
        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(member.getId().toString(), member.getNickname());
        return jwtToken;
    }

    @Override
    public String register(MemberRegisterVO memberRegisterVO) {
        //获取注册的数据
        //String code = registerVo.getCode(); //验证码
        String mobile = memberRegisterVO.getMobile(); //手机号
        //String nickname = registerVo.getNickname(); //昵称
        String password = memberRegisterVO.getPassword(); //密码
        return null;
    }
}
